﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace StateManager
{
    class SingleGameScreen : GenericScreenCode
    {

        ContentManager content;
        private Level level;

        int lives;
        bool allPlacesLanded;


        KeyboardState kb;
        GamePadState gb;

        
        int score = 0; // the score of the player is held in this screen as to pass it on between levels
        int levelNum; // the number of the level, defaults to zero

        public SingleGameScreen()
        {
            lives = 3; // when this screen has been created, the lives are set to three
            levelNum = 0; // the level is zero
        }

        public override void LoadContent()
        {
 
            content = new ContentManager(SManager.Game.Services, "Content");


            // the mediaplayer code is put in a try catch statement because of a bug with the media player where it will throw an exception
            // for some reason.  i should put the link here for reference
            try
            {
                MediaPlayer.IsRepeating = true;
                MediaPlayer.Play(content.Load<Song>("sounds/froggertrack"));
                MediaPlayer.Volume = 0.5f;
            }
            catch { }

            loadNextLevel(); // the next level is loaded here 

            
        }

        public override void UnloadContent()
        {
            MediaPlayer.Stop(); // when this screen is exiting, the player is told to stop playing the media file
            content.Unload(); // the screen is then unloaded
        }

        public void loadNextLevel()
        {
            levelNum++; // levelnum starts at 0 and increments when this method is called
            using (Stream fs = TitleContainer.OpenStream(@"Content/levels/level.txt")) // the file stream opens the level text file to get the layout 
            {
                level = new SinglePlayerLevel(SManager.Game.Services, fs, SManager.SpriteBatch, 
                    SManager.GraphicsDevice.Viewport.Width,
                    SManager.GraphicsDevice.Viewport.Height,
                    levelNum, ref score, ref lives);
                // a new level is created and graphics, services and other vars are passed
                // score and lives are references because there are checked in the update method below
                // to see if the lives have reached zero.  This screen checks for the winning and losing conditions.
                // when a new level is loaded, the amount of lives and score from the previous level are loaded in.
            }
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool setFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, setFocus, false); // the other screens are run as well.
            kb = Keyboard.GetState();
            gb = GamePad.GetState(PlayerIndex.One);

            // the keyboard and gamepad states are updated in this loop

            if (IsActive)
            {
                level.updateLevel(kb,gb, gameTime, ref score, ref lives, ref allPlacesLanded);
                // the level is updated and the score, lives and landing pads are checked

                if (allPlacesLanded && levelNum == 3) // if all the places are filled and it's the last level
                {
                    SManager.AddScreen(new GameOverScreen("You have beating of the gameses"), ControllingPlayer);
                    //a message screen is shown and the player is congratulated
                    // when the player presses a or enter, he is brought back to the main menu
                }
                else if (allPlacesLanded) // if all the places are landed 
                {
                    level.Dispose(); // the current level is discarded
                    allPlacesLanded = false; // the places are reset
                    SManager.AddScreen(new ContinueScreen("You have beaten level" + levelNum),ControllingPlayer);
                    // a message shows on screen informing the player he has beaten the level he was on
                    loadNextLevel(); // the next level is loaded
                }
                else if (lives <= 0) // if the player runs out of lives
                    SManager.AddScreen(new GameOverScreen("Game Over\n Your score is " + score.ToString()), ControllingPlayer);
                // the player is told that the game is over and is given his/her score
            }


        }



        public override void HandleInput(Input input)
        {
            if (input.IsPauseGame(ControllingPlayer)) // if the player presses pause to bring up the pause screen
            {
                MediaPlayer.Volume = 0.1f; // the volume on the media player is set to 10%
                SManager.AddScreen(new PauseScreen(), ControllingPlayer); // the pause screen is loaded into the screens stack
            }

        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            SManager.GraphicsDevice.Clear(ClearOptions.Target,
                                               Color.CornflowerBlue, 0, 0);

            SpriteBatch sB = SManager.SpriteBatch;

            sB.Begin();

            level.Draw(gameTime, sB); // the level is drawn in this method

            sB.End();
        }
    }
}
